package kyuu.acshmily.comment
{
	import mx.core.Container;

	/**
	 * 弹幕管理器
	 */
	public class CommentsManager
	{
		// 过滤器
		private static var _filter:CommentFilter = new CommentFilter();
		// 评论列表
		private static var _comments:Array = null;
		// 弹幕舞台列表
		private static var _stages:Array = null;
		// 
		private static var _playStatus:Boolean = false;

		public function CommentsManager(){}

		/**
		 * 加载弹幕XML 
		 * @param xml - 弹幕XML
		 */
		public static function loadXML(xml:XML):void{
			if(xml==null) return;
			if(_comments!=null) return;

			// 将XML中的节点加入到评论数组中
			var nodes:XMLList = xml.child("comment");
			_comments = new Array();
			for each(var node:XML in nodes){
				node.@shown = 0;
				_comments.push(node);
			}
		}

		/**
		 * 初始化弹幕舞台
		 * @param cont - 弹幕舞台的容器
		 */
		public static function initStages(cont:Container):void{
			if(cont == null) return;
			if(_stages != null) return;
			
			// 向主程序添加弹幕舞台
			_stages = new Array(6);
			for(var i:uint = 0; i < 6; i++){
				var stg:CommentsStage = new CommentsStage(i + 1);
				cont.addChild(stg);
				stg.x = 0;
				stg.y = 0;
				stg.width = cont.width;
				stg.height = cont.height;
				_stages[i] = stg;
			}
		}

		/**
		 * 通知弹幕舞台更新。弹幕舞台根据传入的时间，增加需要显示的弹幕。
		 * @param time - 新的时间
		 */
		public static function notifyUpdate(time:Number):void{
			// 检索出当前需要显示的弹幕
			_filter.playTime = time;
			var cmnts:Array = _comments.filter(filterComments, null);
			if(cmnts.length <=0 ) return;

			// 将检索到的弹幕添加到舞台上
			for(var i:int=0; i<cmnts.length; i++){
				var cmntXML:XML = cmnts[i] as XML;
				if(cmntXML == null) continue;

				// 修改标志位
				cmntXML.@shown = 1;
				
				// 过滤特定弹幕
				if(!_filter.showLocked && !_filter.showNormal) continue;
				else if(!_filter.showLocked && cmntXML.@ isLocked>0) continue;
				else if(!_filter.showNormal && cmntXML.@ isLocked<=0) continue;

				// 获取对应的舞台
				var stg:CommentsStage = _stages[cmntXML.@mode - 1] as CommentsStage;
				if(stg == null) continue;

				// 添加该弹幕
				stg.addComment(cmntXML, _playStatus);
			}
		}

		/**
		 * 过滤弹幕的回调函数：只做时间过滤，其他处理不在此处
		 * @param item - 一条弹幕信息
		 * @param index - 信息所以
		 * @param array - 数组
		 */
		private static function filterComments(item:XML, index:int, array:Array):Boolean{
			var fOK:Boolean = (item.playTime <= _filter.playTime) && (item.@shown <= 0);
			return fOK;
		}

		/**
		 * 通知用户调整了视频时间
		 * 弹幕管理器会重置弹幕标志位，并通知舞台清空已有的弹幕
		 */
		public static function notifySeek(seekTime:Number):void{
			var i:int;
			// 重置弹幕的显示标识
			for(i=0; _comments!=null && i<_comments.length; i++){
				var cmnt:XML = _comments[i] as XML;
				if(cmnt == null) continue;

				// 根据seekTime重置标志位
				if(cmnt.playTime >= seekTime)
					cmnt.@shown = 0;
				else
					cmnt.@shown = 1;
			}

			// 通知所有弹幕舞台，清空弹幕
			for(i=0; _stages!=null && i<_stages.length; i++){
				var cs:CommentsStage = _stages[i] as CommentsStage;
				if(cs == null) continue;
				cs.clearComments();
			}
		}

		/**
		 * 通知暂停弹幕的动态效果
		 */
		public static function notifyPause():void{
			_playStatus = false;
			if(_stages == null) return;
			for(var i:int=0; i<_stages.length; i++){
				var cs:CommentsStage = _stages[i] as CommentsStage;
				if(cs == null) continue;
				cs.notifyPause();
			}
		}

		/**
		 * 通知恢复弹幕的动态效果
		 */
		public static function notifyResume():void{
			_playStatus = true;
			if(_stages == null) return;
			for(var i:int=0; i<_stages.length; i++){
				var cs:CommentsStage = _stages[i] as CommentsStage;
				if(cs == null) continue;
				cs.notifyResume();
			}
		}

		/**
		 * 通知发生全屏事件
		 */
		public static function notifyFullScreen():void{
			for(var i:int=0; i<_stages.length; i++){
				var cs:CommentsStage = _stages[i] as CommentsStage;
				if(cs == null) continue;
				cs.width = cs.parent.width;
				cs.height = cs.parent.height;
			}
		}

		/**
		 * 设置指定类型的弹幕是否可见
		 */
		public static function setCommentsVisible(locked:Boolean, normal:Boolean):void{
			_filter.showLocked = locked;
			_filter.showNormal = normal;
			
			// 通知所有弹幕舞台，隐藏弹幕
			for(var i:int=0; _stages!=null && i<_stages.length; i++){
				var cs:CommentsStage = _stages[i] as CommentsStage;
				if(cs == null) continue;
				
				cs.setCommentsVisible(locked, normal);
			}
		}

		/**
		 * 添加一条弹幕
		 */
		public static function addComment(cmntInfo:XML):void{
			cmntInfo.@hasBorder = 1;
			cmntInfo.@shown = 0;
			_comments.push(cmntInfo);

			notifyUpdate(cmntInfo.playTime);
		}
	}
}